/* 
*  Copyright (C) 2009-2010 WCG Project
*
*  This program is free software; you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by the
*  Free Software Foundation; either version 2 of the License, or (at
*  your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but
*  WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software Foundation,
*  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*  In addition, as a special exception, the author gives permission to
*  link the code of this program with the Half-Life Game Engine ("HL
*  Engine") and Modified Game Libraries ("MODs") developed by Valve,
*  L.L.C ("Valve"). You must obey the GNU General Public License in all
*  respects for all of the code used other than the HL Engine and MODs
*  from Valve. If you modify this file, you may extend this exception
*  to your version of the file, but you are not obligated to do so. If
*  you do not wish to do so, delete this exception statement from your
*  version.
*/



#define ULT_SEARCHTIME		50			// Translates to 5 seconds

// This isn't actually called when they join spectator :/
public cmd_Teamselect( id, key )
{
	PlayerInfo[id][PI_ChangingTeam] = true
	return PLUGIN_CONTINUE
}

public cmd_Jointeam( id )
{
	PlayerInfo[id][PI_ChangingTeam] = true
	return PLUGIN_CONTINUE
}

// This is here to prevent the user from forcing a ResetHUD event
public cmd_fullupdate( id )
{
	log_amx( "FULLUPDATE: %s attempted to use the fullupdate command!", PlayerInfo[id][PI_PlayerName] )
	return PLUGIN_HANDLED
}


//-------------------------------------------
// cmd_Drop
//
//-------------------------------------------
public cmd_Drop( id )
{
	SHARED_SaveWeapons( id )
	return PLUGIN_CONTINUE
}


// Called when the user buys a he grenade
public cmd_hegren( id )
{ 
	// Make sure we even have grenade protection on
	if ( !get_pcvar_num( CVAR_grenade_protection ) )
		return PLUGIN_CONTINUE

	// Then they shouldn't be buying a grenade anyway
	if ( !cs_get_user_buyzone( id ) )
		return PLUGIN_HANDLED

	// User can only buy one grenade per round
	if ( PlayerInfo[id][PI_HECount] >= 1 )
	{ 
		client_print( id, print_center, "%L", id, "ONLY_ONE_GRENADE_PER_ROUND" )
		return PLUGIN_HANDLED
	}
	
	// Increment since they bought their grenade
	else
	{
		PlayerInfo[id][PI_HECount]++
		return PLUGIN_CONTINUE
	}

	return PLUGIN_CONTINUE
} 


public cmd_Ability(id)
{
	// User can't use ability when dead or during freezetime
	if( GameInfo[GI_FreezeTime] || !is_user_alive(id) )
		return PLUGIN_HANDLED

	new iSkillLevel
	
	new iSkillID = PlayerInfo[id][PI_SelectedAbility]

	if( IsValidSkill( iSkillID ) )
		iSkillLevel = SM_GetSkillLevel( id, iSkillID, 3 )

	// User has no ability!
	if( !IsValidSkill( iSkillID ) || iSkillLevel == 0 )
	{
		WCG_StatusText( id, TXT_ULTIMATE, HUD_ULTIMATE, "Ability not found." )
		client_cmd( id, "speak %s", g_szSounds[SOUND_ERROR] )
		return PLUGIN_HANDLED
	}

	switch( PlayerInfo[id][PI_Class] )
	{
//		case CLASS_ROGUE:			ROGUE_TriggerAbility( id, iSkillID )
		case CLASS_PALADIN:			PALADIN_TriggerAbility( id, iSkillID )
		case CLASS_SHAMAN:			SHAMAN_TriggerAbility( id, iSkillID )
		case CLASS_DRUID:			DRUID_TriggerAbility( id, iSkillID )
//		case CLASS_WARLOCK:			WARLOCK_TriggerAbility( id, iSkillID )
		case CLASS_HUNTER:			HUNTER_TriggerAbility( id, iSkillID )
//		case CLASS_PRIEST:			PRIEST_TriggerAbility( id, iSkillID )
		case CLASS_MAGE:			MAGE_TriggerAbility( id, iSkillID )
	}
		
	return PLUGIN_HANDLED
}



public cmd_Ultimate(id)
{
	// User can't use ultimate when dead
	if ( GameInfo[GI_FreezeTime] || !is_user_alive(id) )
		return PLUGIN_HANDLED

	new iSkillLevel

	new iSkillID = PlayerInfo[id][PI_SelectedUltimate]

	if( IsValidSkill( iSkillID ) )
		iSkillLevel = SM_GetSkillLevel( id, iSkillID, 3 )

	// User has no ultimate!
	if( iSkillLevel == 0 )
	{
		WCG_StatusText( id, TXT_ULTIMATE, HUD_ULTIMATE, "%L", id, "ULTIMATE_NOT_FOUND" )
		client_cmd( id, "speak %s", g_szSounds[SOUND_ERROR] )
		return PLUGIN_HANDLED
	}
	
	switch( PlayerInfo[id][PI_Class] )
	{
		case CLASS_ROGUE:			ROGUE_TriggerUltimate( id, iSkillID )
		case CLASS_PALADIN:			PALADIN_TriggerUltimate( id, iSkillID )
		case CLASS_SHAMAN:			SHAMAN_TriggerUltimate( id, iSkillID )
		case CLASS_DRUID:			DRUID_TriggerUltimate( id, iSkillID )
		case CLASS_WARLOCK:			WARLOCK_TriggerUltimate( id, iSkillID )
		case CLASS_HUNTER:			HUNTER_TriggerUltimate( id, iSkillID )
		case CLASS_PRIEST:			PRIEST_TriggerUltimate( id, iSkillID )
		case CLASS_MAGE:			MAGE_TriggerUltimate( id, iSkillID )
	}
	
	return PLUGIN_HANDLED
}


public CMD_Handler( id )
{

	new szCmd[32]

	read_argv( 0, szCmd, 31 )

	CMD_Handle( id, szCmd, false )

	return PLUGIN_HANDLED
}

public cmd_Say( id )
{
	
	new szSaid[32]
	read_args( szSaid, 31 )

	remove_quotes( szSaid )

	CMD_Handle( id, szSaid, true )

	return PLUGIN_CONTINUE
}

// Command handler
CMD_Handle( id, szCmd[], bool:bThroughSay )
{
	if ( CMD_Equal( szCmd, "wcghelp" ) )
		MOTD_War3help(id)

	else if ( CMD_Equal( szCmd, "wcgmenu" ) )
		MENU_WCGMenu( id )

	// Change the users class
	else if ( CMD_Equal( szCmd, "changeclass" ) )
		WCG_ChangeClassStart( id )

	// Display select skill menu
	else if ( CMD_Equal( szCmd, "selectskills" ) || CMD_Equal( szCmd, "selectskill" ) )
		MENU_SelectSkill( id )

	else if ( CMD_Equal( szCmd, "selectattrib" ) || CMD_Equal( szCmd, "selectattribs" ) || CMD_Equal( szCmd, "selectattribute" ) || CMD_Equal( szCmd, "selectattributes" ) )
		MENU_SelectAttribute( id )


	else if ( CMD_Equal( szCmd, "playerskills" ) )
		MOTD_PlayerSkills( id, bThroughSay )

	else if ( CMD_Equal( szCmd, "ms" ) || CMD_Equal( szCmd, "movespeed" ) )
		client_print( id, print_chat, "Move Speed: %0.0f", fm_get_user_maxspeed( id ) )

	else if ( CMD_Equal( szCmd, "skillsinfo" ) )
		MOTD_SkillsInfo( id )

	else if ( CMD_Equal( szCmd, "attribinfo" ) || CMD_Equal( szCmd, "attribsinfo" ) || CMD_Equal( szCmd, "attributesinfo" ))
		MOTD_AttributesInfo( id )

	else if ( CMD_Equal( szCmd, "levitation" ) )
	{
		if ( SM_GetSkillLevel( id, SKILL_LEVITATION ) <= 0 )
		{
			client_print( id, print_chat, "%s You do not have levitation!", g_MODclient )

			return
		}

		if ( g_bLevitation[id] )
		{
			g_bLevitation[id] = false
			
			client_print( id, print_chat, "%s Levitation disabled, type /levitation to re-enable", g_MODclient )
		}
		else
		{
			g_bLevitation[id] = true
			
			client_print( id, print_chat, "%s Levitation enabled, type /levitation to disable", g_MODclient )
		}

		PLAYER_UpdateGravity( id )
	}

	else if ( CMD_Equal( szCmd, "level" ) )
	{
		WCG_ShowClassInfo( id )
		WC3_ShowBar( id )
	}

	else if ( CMD_Equal( szCmd, "shopmenu" ) )
	{
		MENU_Shopmenu( id, 0 )
	}

	else if ( CMD_Equal( szCmd, "resetxp" ) )
	{
		MENU_ResetXP( id )
	}

	else if ( CMD_Equal( szCmd, "itemsinfo" ) )
	{
		MOTD_ItemsInfo( id, 0 )
	}

	else if ( CMD_Equal( szCmd, "savexp" ) )
	{
       client_print( id, print_chat, "%s XP is saved automatically, you do not need to type this command", g_MODclient )
	}

	else if ( CMD_Equal( szCmd, "resetskills" ) )
	{
		client_print( id, print_center, "%L", id, "SKILLS_RESET_NEXT_ROUND" )
		PlayerInfo[id][PI_ResetSkills] = true
	}

	else if ( CMD_Equal( szCmd, "resetattribs" ) || CMD_Equal( szCmd, "resetattributes" ) )
	{
		client_print( id, print_center, "%L", id, "ATTRIBS_RESET_NEXT_ROUND" )
		PlayerInfo[id][PI_ResetAttributes] = true
	}


	// Ability to buy items through console commands
	else if ( CMD_Equal( szCmd, "ankh" ) )
	{
		if ( ITEM_MenuCanBuyCheck(id) )ITEM_Buy( id, ITEM_ANKH )
	}

	else if ( CMD_Equal( szCmd, "boots" ) )
	{
		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_BOOTS )
	}

	else if ( CMD_Equal( szCmd, "claws" ) )
	{
		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_CLAWS )
	}

//	else if ( CMD_Equal( szCmd, "cloak" ) )
//	{
//		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_CLOAK )
//	}

	else if ( CMD_Equal( szCmd, "mask" ) )
	{
		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_MASK )
	}

	else if ( CMD_Equal( szCmd, "necklace" ) )
	{
		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_NECKLACE )
	}

	else if ( CMD_Equal( szCmd, "frost" ) )
	{
		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_FROST )
	}

	else if ( CMD_Equal( szCmd, "health" ) )
	{
		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_HEALTH )
	}

	else if ( CMD_Equal( szCmd, "tome" ) )
	{
		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_TOME )
	}

	if ( CMD_Equal( szCmd, "itemsinfo2" ) )
	{
		MOTD_ItemsInfo( id, 9 )
	}

//	else if ( CMD_Equal( szCmd, "scroll" ) )
//	{
//		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_SCROLL )
//	}

//	else if ( CMD_Equal( szCmd, "helm" ) )
//	{
//		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_HELM )
//	}

	else if ( CMD_Equal( szCmd, "amulet" ) )
	{
		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_AMULET )
	}

	else if ( CMD_Equal( szCmd, "socks" ) )
	{
		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_SOCK )
	}

	else if ( CMD_Equal( szCmd, "gloves" ) )
	{
		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_GLOVES )
	}

	else if ( CMD_Equal( szCmd, "rings" ) )
	{
		if ( ITEM_MenuCanBuyCheck( id ) ) ITEM_BuyRings( id )
	}

//	else if ( CMD_Equal( szCmd, "chameleon" ) )
//	{
//		if (ITEM_MenuCanBuyCheck(id)) ITEM_Buy( id, ITEM_CHAMELEON )
//	}

	else if ( CMD_Equal( szCmd, "shopmenu2" ) )
	{
		MENU_Shopmenu( id, 9 )
	}


	//
	// Legacy cmds
	//
	else if ( CMD_Equal( szCmd, "war3help" ) )
		MOTD_War3help(id)

	else if ( CMD_Equal( szCmd, "war3menu" ) )
		MENU_WCGMenu( id )

	else if ( CMD_Equal( szCmd, "changerace" ) )
		WCG_ChangeClassStart( id )

	return
}

// Function will check if the first string is equal to the second (checks for NAME or /NAME)
CMD_Equal( szCmd[], szCorrectCmd[] )
{
	new szTmp[64]
	formatex( szTmp, 63, "/%s", szCorrectCmd )

	new bool:bValid = equali( szCmd, szTmp ) || equali( szCmd, szCorrectCmd )
	
	return bValid
}
